package com.huitone.gddw.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class DateTimeUtils {
	
	private static SimpleDateFormat format;
	
	public static String dateToStr(Date date) {
		return dateToStr(date, null);
	}
	
	public static Date strToDate(String str) {
		return strToDate(str, null);
	}

	public static String dateToStr(Date date, String pattern) {
		
		SimpleDateFormat format = null;
		
		if (StringUtils.isEmpty(pattern)) {
			format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//			format = format;
		} else {
			format = new SimpleDateFormat(pattern);
		}
		
		return format.format(date);
	}
	
	public static Date strToDate(String str, String pattern) {
		
		SimpleDateFormat format = null;
		
		if (StringUtils.isEmpty(pattern)) {
			format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//			format = format;
		} else {
			format = new SimpleDateFormat(pattern);
		}
		
		try {
			return format.parse(str);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		
		return null;
	}
	
	/**
	 * 当前时间是否在startTime-endTime区间内
	 * @param startTime 0.00 - 23.00
	 * @param endTime 0.00 - 23.00
	 * @return
	 */
	public static boolean isInInterval(float startTime, float endTime) {
		
		boolean result = false;
		
		Date date = new Date();
		
		Integer currentM = StringUtils.strToInteger(DateTimeUtils.dateToStr(date, "mm"));
		
		Integer currentH = StringUtils.strToInteger(DateTimeUtils.dateToStr(new Date(), "H"));
		
		float currentTime = currentH + currentM / 60f;
		
		if (endTime > startTime) {
			if (currentTime >= startTime && currentTime < endTime) {
				result = true;
			}
		} else {
			if ((currentTime >= startTime && currentTime < 24) || (currentTime >= 0 && currentTime < endTime)) {
				result = true;
			}
		}
		
		return result;
	}
	
	public static void main(String[] args) {
		
		Date date = new Date();
		
		Integer currentM = StringUtils.strToInteger(DateTimeUtils.dateToStr(date, "mm"));
		
		Integer currentH = StringUtils.strToInteger(DateTimeUtils.dateToStr(new Date(), "H"));
		
		float currentTime = currentH + currentM / 60f;
		
		System.out.println(currentM);
		System.out.println(currentH);
		System.out.println(currentTime);
		
	}
}
